package cn.iocoder.yudao.module.agent.dal.mysql.brokeragerecord;

import java.time.LocalDateTime;
import java.util.*;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.module.agent.dal.dataobject.brokeragerecord.AgentBrokerageRecordDO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.apache.ibatis.annotations.Mapper;
import cn.iocoder.yudao.module.agent.controller.admin.brokeragerecord.vo.*;

/**
 * 代理人佣金记录 Mapper
 *
 * @author 曹建伟
 */
@Mapper
public interface AgentBrokerageRecordMapper extends BaseMapperX<AgentBrokerageRecordDO> {

    default PageResult<AgentBrokerageRecordDO> selectPage(AgentBrokerageRecordPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<AgentBrokerageRecordDO>()
                .eqIfPresent(AgentBrokerageRecordDO::getAgentUserId, reqVO.getAgentUserId())
                .eqIfPresent(AgentBrokerageRecordDO::getBizId, reqVO.getBizId())
                .eqIfPresent(AgentBrokerageRecordDO::getStatus, reqVO.getStatus())
                .eqIfPresent(AgentBrokerageRecordDO::getFrozenDays, reqVO.getFrozenDays())
                .betweenIfPresent(AgentBrokerageRecordDO::getUnfreezeTime, reqVO.getUnfreezeTime())
                .betweenIfPresent(AgentBrokerageRecordDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(AgentBrokerageRecordDO::getId));
    }

    default List<AgentBrokerageRecordDO> selectList(AgentBrokerageRecordExportReqVO reqVO) {
        return selectList(new LambdaQueryWrapperX<AgentBrokerageRecordDO>()
                .eqIfPresent(AgentBrokerageRecordDO::getBizId, reqVO.getBizId())
                .eqIfPresent(AgentBrokerageRecordDO::getStatus, reqVO.getStatus())
                .eqIfPresent(AgentBrokerageRecordDO::getFrozenDays, reqVO.getFrozenDays())
                .betweenIfPresent(AgentBrokerageRecordDO::getUnfreezeTime, reqVO.getUnfreezeTime())
                .betweenIfPresent(AgentBrokerageRecordDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(AgentBrokerageRecordDO::getId));
    }

    default List<AgentBrokerageRecordDO> selectListByStatusAndUnfreezeTimeLt(Integer status, LocalDateTime unfreezeTime) {
        return selectList(new LambdaQueryWrapper<AgentBrokerageRecordDO>()
                .eq(AgentBrokerageRecordDO::getStatus, status)
                .lt(AgentBrokerageRecordDO::getUnfreezeTime, unfreezeTime));
    }

    default int updateByIdAndStatus(Long id, Integer status, AgentBrokerageRecordDO updateObj) {
        return update(updateObj, new LambdaQueryWrapper<AgentBrokerageRecordDO>()
                .eq(AgentBrokerageRecordDO::getId, id)
                .eq(AgentBrokerageRecordDO::getStatus, status));
    }

    default AgentBrokerageRecordDO selectByBizIdAndUserId(Long bizId, Long sourceUserId) {
        return selectOne(AgentBrokerageRecordDO::getBizId, bizId,
                AgentBrokerageRecordDO::getSourceUserId, sourceUserId);
    }

}
